package com.lwy.qiuchao.member.service.impl;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.lwy.qiuchao.member.exception.PhoneExistException;
import com.lwy.qiuchao.member.exception.UserNameExistException;
import com.lwy.qiuchao.member.vo.MemberInfoVo;
import com.lwy.qiuchao.member.vo.MemberLoginVo;
import com.lwy.qiuchao.member.vo.MemberRegistVo;

import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lwy.common.utils.PageUtils;
import com.lwy.common.utils.Query;

import com.lwy.qiuchao.member.dao.MemeberDao;
import com.lwy.qiuchao.member.entity.MemeberEntity;
import com.lwy.qiuchao.member.service.MemeberService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


@Service("memeberService")
public class MemeberServiceImpl extends ServiceImpl<MemeberDao, MemeberEntity> implements MemeberService {


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<MemeberEntity> page = this.page(
                new Query<MemeberEntity>().getPage(params),
                new QueryWrapper<MemeberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void regist(MemberRegistVo memeberRegistVo) {
        MemeberDao baseMapper = this.baseMapper;
        MemeberEntity memeberEntity = new MemeberEntity();

        //检查用户名和手机号是否唯一，为了让controller能感知异常
        checkPhoneUnique(memeberRegistVo.getPhone());
        checkUsernameUnique(memeberRegistVo.getUserName());


        String userName = memeberRegistVo.getUserName();
        String phone = memeberRegistVo.getPhone();
        memeberEntity.setUsername(userName);
        memeberEntity.setPhone(phone);

        //设置密码的时候要进行加密设置
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encode = passwordEncoder.encode(memeberRegistVo.getPassword());
        memeberEntity.setPassword(encode);
        baseMapper.insert(memeberEntity);

    }

    @Override
    public MemberInfoVo getMemberInfo(Long mId) {
        return this.baseMapper.getMemberInfo(mId);
    }

    @Override
    public MemeberEntity login(MemberLoginVo memberLoginVo) {



        String userName = memberLoginVo.getUsername();
        System.out.println(userName);
        String password = memberLoginVo.getPassword();
        MemeberDao memberDao = this.baseMapper;
        MemeberEntity memberEntity = memberDao.selectOne(new QueryWrapper<MemeberEntity>().eq("username", userName));
        if (memberEntity == null) {
            //登陆失败
            return null;
        } else {
            String passwordDb = memberEntity.getPassword();
            BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
            boolean matches = passwordEncoder.matches(password, passwordDb);
            if (matches) {
                if (memberEntity.getToken() == null) {
                    String token = IdWorker.getTimeId();
                    memberEntity.setToken(token);
                    memberDao.updateById(memberEntity);
                    return memberEntity;
                }
                return memberEntity;
            }
        }
        return null;
    }

    @Override
    public MemeberEntity hasToken(String username) {
      return  this.baseMapper.hasToken(username);
    }

    @Override
    public MemeberEntity memberInfoByToken(String token) {
        return this.baseMapper.memberInfo(token);
    }

    @Override
    public int modifyInfo(MemeberEntity memeberEntity) {
        MemeberEntity entity = this.baseMapper.selectOne(new QueryWrapper<MemeberEntity>().eq("m_id", memeberEntity.getMId()));
        entity.setGender(memeberEntity.getGender());
        entity.setCity(memeberEntity.getCity());

        entity.setHeader(memeberEntity.getHeader());
        entity.setSign(memeberEntity.getSign());
        entity.setBirthday(memeberEntity.getBirthday());
        int i = this.baseMapper.updateById(entity);
        return i;
    }




    public void checkUsernameUnique(String userName) throws UserNameExistException {
        MemeberDao memberDao = this.baseMapper;
        Integer username = memberDao.selectCount(new QueryWrapper<MemeberEntity>().eq("username", userName));
        if (username > 0) {
            throw new UserNameExistException();
        }

    }

    public void checkPhoneUnique(String phone) throws PhoneExistException {
        MemeberDao memberDao = this.baseMapper;
        Integer phone1 = memberDao.selectCount(new QueryWrapper<MemeberEntity>().eq("phone", phone));
        if (phone1 > 0) {
            throw new PhoneExistException();
        }

    }

}